package com.the_ring.chapter09.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.the_ring.chapter09.R;
import com.the_ring.chapter09.adapter.RecyclerGridAdapter;
import com.the_ring.chapter09.bean.ItemInfo;
import com.the_ring.chapter09.widget.BannerPager;

import java.util.ArrayList;
import java.util.List;


public class FirstFragment extends Fragment {


    private Context context;

    public FirstFragment(Context context) {
        // Required empty public constructor
        this.context = context;
    }


    public static FirstFragment newInstance(Context context) {
        FirstFragment fragment = new FirstFragment(context);
//        Bundle args = new Bundle();
//        args.putString(ARG_PARAM1, param1);
//        args.putString(ARG_PARAM2, param2);
//        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @SuppressLint("MissingInflatedId")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_first, container, false);

        BannerPager banner = view.findViewById(R.id.banner);
        // 获取广告轮播条的布局参数
        ViewGroup.LayoutParams params =  banner.getLayoutParams();
        Display display = getActivity().getWindowManager().getDefaultDisplay();
        params.height = (int) (display.getWidth() * 250f / 640f);
        banner.setLayoutParams(params);
        banner.setImageViews(getImageList());
        banner.start();

        // 首页分区
        RecyclerView marketRl = view.findViewById(R.id.rl_market);
        GridLayoutManager manager = new GridLayoutManager(context, 20);
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position < 10) return 4;
                else if (position == 10 || position == 11) return 10;
                else return 5;
            }
        });
        marketRl.setLayoutManager(manager);
        marketRl.setAdapter(new RecyclerGridAdapter(context, getMarketList()));

        return view;
    }

    private List<Integer> getImageList() {
        List<Integer> imageList = new ArrayList<>();
        imageList.add(R.drawable.banner_1);
        imageList.add(R.drawable.banner_2);
        imageList.add(R.drawable.banner_3);
        imageList.add(R.drawable.banner_4);
        imageList.add(R.drawable.banner_5);
        imageList.add(R.drawable.banner_6);
        return imageList;
    }

    private List<ItemInfo> getMarketList() {
        List<ItemInfo> infoList = new ArrayList<>();
        infoList.add(new ItemInfo(R.drawable.market_0, "商场"));
        infoList.add(new ItemInfo(R.drawable.market_1, "超市"));
        infoList.add(new ItemInfo(R.drawable.market_2, "百货"));
        infoList.add(new ItemInfo(R.drawable.market_3, "便利店"));
        infoList.add(new ItemInfo(R.drawable.market_4, "地摊"));
        infoList.add(new ItemInfo(R.drawable.market_5, "食杂店"));
        infoList.add(new ItemInfo(R.drawable.market_6, "饭店"));
        infoList.add(new ItemInfo(R.drawable.market_7, "餐厅"));
        infoList.add(new ItemInfo(R.drawable.market_8, "会所"));
        infoList.add(new ItemInfo(R.drawable.market_9, "菜市场"));
        infoList.add(new ItemInfo(R.drawable.market_10, "猜你喜欢"));
        infoList.add(new ItemInfo(R.drawable.market_11, "大牌驾到"));
        infoList.add(new ItemInfo(R.drawable.market_12, "买哪个"));
        infoList.add(new ItemInfo(R.drawable.market_13, "刚想了"));
        infoList.add(new ItemInfo(R.drawable.market_14, "先下单"));
        infoList.add(new ItemInfo(R.drawable.market_15, "包你喜欢"));
        return infoList;
    }

}